package com.fs.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.mapper.GoodsCategoryMapper;
import com.fs.pojo.GoodsCategory;
import com.fs.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class GoodsCategorySerivceImpl implements GoodsCategorySerivce {
    @Autowired
    private GoodsCategoryMapper goodsCategoryDao;

    @Override
    public SysResult findItemCatList(Integer level) {
        Map<Integer, List<GoodsCategory>> map=getMap(level);
        if(level==1)return SysResult.success(map.get(0));
        if(level==2)return SysResult.success(twoLevel(map));
        return SysResult.success(threeLevel(map));
    }

    @Override
    @Transactional
    public SysResult updateStatus(GoodsCategory itemCat) {
        Date now = new Date();
        itemCat.setUpdated(now);
        goodsCategoryDao.updateById(itemCat);
        return SysResult.success();
    }

    @Override
    @Transactional
    public SysResult updateItemCat(GoodsCategory itemCat) {
        Date now = new Date();
        itemCat.setUpdated(now);
        goodsCategoryDao.updateById(itemCat);
        return SysResult.success();
    }

    @Override
    @Transactional
    public SysResult saveItemCat(GoodsCategory itemCat) {
        Date now = new Date();
        itemCat.setStatus(true)
                .setCreated(now)
                .setUpdated(now);
        goodsCategoryDao.insert(itemCat);
        return SysResult.success();
    }

    @Override
    @Transactional
    public SysResult deleteItemCat(GoodsCategory itemCat) {
        if(itemCat.getId()==null){
            itemCat.setId(-99);
        }
        QueryWrapper<GoodsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid",itemCat.getId());
        List twoIdsList = goodsCategoryDao.selectObjs(queryWrapper);

        queryWrapper.clear();
        queryWrapper.eq("id",itemCat.getId())
                .or()
                .eq("pid",itemCat.getId())
                .or()
                .in(twoIdsList.size()>0,"pid",twoIdsList);
        goodsCategoryDao.delete(queryWrapper);
        return SysResult.success();
    }

    private Map<Integer, List<GoodsCategory>> getMap(Integer level) {
        QueryWrapper<GoodsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.le("level",level);
        Map<Integer,List<GoodsCategory>> map=new HashMap<>();
        List<GoodsCategory> allList = goodsCategoryDao.selectList(queryWrapper);
        if(allList==null)return map;
        for (GoodsCategory v : allList) {
            Integer parentId = v.getPid();
            if(!map.containsKey(parentId)){
                ArrayList<GoodsCategory> list = new ArrayList<>();
                list.add(v);
                map.put(parentId,list);
            }else {
                map.get(parentId).add(v);
            }
        }

        return map;
    }

    private List<GoodsCategory> twoLevel(Map<Integer, List<GoodsCategory>> map) {
        List<GoodsCategory> level1 = map.get(0);
        if(level1==null)return null;
        for (GoodsCategory v1 : level1) {
            v1.setChildren(map.get(v1.getId()));
        }
        return level1;
    }

    private List<GoodsCategory> threeLevel(Map<Integer, List<GoodsCategory>> map) {
        List<GoodsCategory> level1 = map.get(0);
        if(level1==null)return null;
        for (GoodsCategory v1 : level1) {
            List<GoodsCategory> list2 = map.get(v1.getId());
            if(list2==null)continue;
            for (GoodsCategory v2 : list2) {
                v2.setChildren(map.get(v2.getId()));
            }
            v1.setChildren(list2);
        }
        return level1;
    }
//    @Override
//    @Transactional
//    public ResultVO add(GoodsCategoryEntity entity) {
//        goodsCategoryDao.insert(entity);
//        return ResultVO.success();
//    }
//
//    @Override
//    @Transactional
//    public ResultVO edit(GoodsCategoryEntity entity) {
//        goodsCategoryDao.updateById(entity);
//        return ResultVO.success();
//    }
//
//    @Override
//    public ResultVO list(CommRequestVO request) {
//        if(request==null)return ResultVO.fail();
//
//        int pageNum = request.getPageNum();
//        int pageSize = request.getPageSize();
//
//        if(pageNum<1)pageNum=1;
//        if(pageSize<1)pageNum=10;
//
//        Page<GoodsCategoryEntity> page = new Page<>(pageNum, pageSize);
//
//
//        QueryWrapper<GoodsCategoryEntity> queryWrapper = new QueryWrapper<>();
//        queryWrapper.like(StrUtil.isNotBlank(request.getQuery()),"username",request.getQuery())
//                .orderByDesc("id");
//        page=goodsCategoryDao.selectPage(page,queryWrapper);
//        request.setTotal(page.getTotal()).setRows(page.getRecords());
//
//        return ResultVO.success(request);
//    }
//
//    @Override
//    @Transactional
//    public ResultVO delById(Integer id) {
//        goodsCategoryDao.deleteById(id);
//        return ResultVO.success();
//    }
}
